package com.ganmiao.utils;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class PdfUtil {


    /**
     * 利用模板生成pdf保存到某路径下
     */
    public static void pdfOut(String oldPdfPath, String newPdfPath, Map<String, String> signMap, Map<String, String> dataMap) throws IOException, DocumentException {
        File newFile = new File(newPdfPath);
        if (!newFile.exists()) {
            try {
                newFile.createNewFile();
            } catch (IOException e) {
                log.info(e.getMessage(), e);
            }
        }
        //pdf模板文件
        InputStream input = new FileInputStream(oldPdfPath);
        //生成的新文件
        File file = new File(newPdfPath);
        FileOutputStream fos = new FileOutputStream(file);
        PdfReader reader = new PdfReader(input);
        PdfStamper stamper = new PdfStamper(reader, fos);
        // 提取PDF中的表单
        AcroFields form = stamper.getAcroFields();
        //文字类的内容处理
        if (Objects.nonNull(dataMap) && !dataMap.isEmpty()) {
            // 设置中文字体
            String prefixFont = "";
            String os = System.getProperties().getProperty("os.name");
            if (os.startsWith("win") || os.startsWith("Win")) {
                prefixFont = "C:\\Windows\\Fonts" + File.separator;
            } else {
                prefixFont = "/usr/share/fonts/chinese" + File.separator;
            }
            BaseFont baseFont = BaseFont.createFont(prefixFont + "simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//            BaseFont baseFont = BaseFont.createFont();
            form.addSubstitutionFont(baseFont);

            for (String key : dataMap.keySet()) {
                String value = dataMap.get(key);
                //设置字体大小
                form.setFieldProperty(key, "textsize", 10f, null);
                form.setField(key, value);
            }
        }

        //签名图片路径存在才进行图片合并
        if (Objects.nonNull(signMap) && !signMap.isEmpty()) {
            for (String signName : signMap.keySet()) {
                //进行签章的填充
                int pageNo = form.getFieldPositions(signName).get(0).page;
                Rectangle signRect = form.getFieldPositions(signName).get(0).position;
                float x = signRect.getLeft();
                float y = signRect.getBottom();
                //读取图片
                Image image = Image.getInstance(signMap.get(signName));
                //获取操作的页面
                PdfContentByte content = stamper.getOverContent(pageNo);
                // 根据域的大小缩放图片
                image.scaleToFit(signRect.getWidth(), signRect.getHeight());
                // 添加图片
                image.setAbsolutePosition(x, y);
                content.addImage(image);
                fos.flush();
            }
        }
        // 生成PDF
        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        fos.close();
        input.close();
    }

    public static void main(String[] args) throws DocumentException, IOException {
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("合同编号", "1515");

        dataMap.put("甲方名称", "高仕登(河南)教育科技有限公司");
        dataMap.put("甲方地址", "张三");
        dataMap.put("甲方联系人", "张三");
        dataMap.put("甲方联系电话", "张三");
        dataMap.put("甲方电子邮件", "张三");
        dataMap.put("甲方开户行", "张三");
        dataMap.put("甲方户名", "张三");
        dataMap.put("甲方账号", "张三");
        dataMap.put("甲方联行号", "张三");

        dataMap.put("乙方名称", "高仕登(河南)教育科技有限公司");
        dataMap.put("乙方地址", "张三");
        dataMap.put("乙方联系人", "张三");
        dataMap.put("乙方联系电话", "张三");
        dataMap.put("乙方电子邮件", "张三");
        dataMap.put("乙方开户行", "张三");
        dataMap.put("乙方户名", "张三");
        dataMap.put("乙方账号", "张三");
        dataMap.put("乙方联行号", "张三");

        dataMap.put("丙方名称", "高仕登(河南)教育科技有限公司");
        dataMap.put("丙方地址", "广东省广州市天河区");
        dataMap.put("丙方联系人", "张三");
        dataMap.put("丙方联系电话", "张三");
        dataMap.put("丙方开户行", "张三");
        dataMap.put("丙方户名", "张三");
        dataMap.put("丙方账号", "张三");
        dataMap.put("丙方联行号", "张三");

        dataMap.put("合同起始年", "2024");
        dataMap.put("合同起始月", "12");
        dataMap.put("合同起始日", "20");
        dataMap.put("合同终止年", "2025");
        dataMap.put("合同终止月", "12");
        dataMap.put("合同终止日", "19");
        dataMap.put("合同合计年", "1");
        dataMap.put("合同合计月", "0");

        Map<String, String> signMap = new HashMap<>();
        int year = LocalDate.now().getYear();
        int month = LocalDate.now().getMonthValue();
        int day = LocalDate.now().getDayOfMonth();

        String signPath = "C:\\files\\csz.png";
        signMap.put("甲方盖章", signPath);
        dataMap.put("甲方盖章年", Integer.toString(year));
        dataMap.put("甲方盖章月", Integer.toString(month));
        dataMap.put("甲方盖章日", Integer.toString(day));

        signMap.put("丙方盖章", signPath);
        dataMap.put("丙方盖章年", Integer.toString(year));
        dataMap.put("丙方盖章月", Integer.toString(month));
        dataMap.put("丙方盖章日", Integer.toString(day));


        signMap.put("乙方盖章", signPath);
        dataMap.put("乙方盖章年", Integer.toString(year));
        dataMap.put("乙方盖章月", Integer.toString(month));
        dataMap.put("乙方盖章日", Integer.toString(day));

        //模板位置
        String oldPdfPath = "C:\\files\\三方合作代收代付合同.pdf";
        //新生成pdf文件位置
        String newPdfPath = "C:\\files\\三方合作代收代付合同-new.pdf";
        //签名图片位置


        pdfOut(oldPdfPath, newPdfPath, signMap, dataMap);

    }

}
